const fs = require('fs');
const path = require('path');

function readFilePromise(fileName) {
    let promise = new Promise((res,rej)=>{
        fs.readFile(path.join(__dirname,fileName),'utf-8',(err,date)=>{
            if(err){ rej(err) }
            else{ res(date) }
        })
    })
    return promise;
}
function writeFilePromise(fileName,content){
    return new Promise((resolve,reject)=>{
        fs.writeFile(path.join(__dirname,fileName),content,(error)=>{
            if(error){ reject(error) }
            else{
                resolve();
            }
        })
    })
}
function sleep(time){
    return new Promise((res)=>{

        setTimeout(() => {

            res();

        }, time);
    })
}
async function main() {
   try{
    await Promise.all([writeFilePromise('1.txt','测试1'),writeFilePromise('2.txt','测试2')]);
    await sleep(1000);
    const [text1,text2] =  await Promise.all([readFilePromise('1.txt'),readFilePromise('2.txt')]);
    await writeFilePromise('3.txt',`${text1} ${text2}`)
   }catch(error){
        console.log('error==>',error);
   }finally{
       console.log('end');
   }
}
main();